package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.IncomeBranch;
import com.turing.post.organization.util.IncomeBranchDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Dec 24, 2012
 */
@Stateless(mappedName = "organization.impl.IncomeBranchDaoImpl")
@Remote
@Local
public class IncomeBranchDaoImpl implements IncomeBranchDao {
	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public IncomeBranch getIncomeBranchByIncomeBranchId(
			BigInteger incomeBranchId) {
		return _em.find(IncomeBranch.class, incomeBranchId);
	}

	@Override
	public void insertIncomeBranch(IncomeBranch incomeBranch)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, incomeBranch);
	}

	@Override
	public void updateIncomeBranch(IncomeBranch incomeBranch)
			throws PCPSApplicationException {
		GeneralDao.update(_em, incomeBranch);
	}

	//
	// @Override
	// public void deleteIncomeBranchByIncomeBranchId(BigInteger incomeBranchId)
	// throws PCPSApplicationException {
	// String sql = "delete from IncomeBranch where IncomeBranchId="
	// + incomeBranchId;
	// Query query = _em.createNativeQuery(sql);
	// try {
	// query.executeUpdate();
	// } catch (Exception e) {
	// throw new OrganApplicationException(
	// OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
	// OrganRespCode.ISSUE_INSERT_UPDATE, "删除发卡商渠道收益机构失败!");
	// }
	// }

	@SuppressWarnings("unchecked")
	@Override
	public List<IncomeBranch> getIncomeBranchListByIssueCompanyNo(
			String issueCompanyNo) {
		return _em.createNativeQuery(
				"select * from IncomeBranch c where c.IssueCompanyNo = '"
						+ issueCompanyNo + "' order by c.IncomeBranchId",
				IncomeBranch.class).getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<IncomeBranch> getIncomeBranchList(PCPSWebInput webInput,
			Integer page) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "";
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = "select t.* from IncomeBranch t where t.IssueCompanyNo = '"
					+ issueCompanyNo + "'";
		} else {
			sql = "select t.* from IncomeBranch t";
		}
		Query query = _em.createNativeQuery(sql, IncomeBranch.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}
	@Override
	public BigInteger getIncomeBranchTotal(PCPSWebInput webInput) {
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			Query query = _em
					.createNativeQuery("select nvl(count(*),0) from IncomeBranch t where t.IssueCompanyNo = '"
							+ issueCompanyNo + "'");
			return new BigInteger(query.getSingleResult().toString());
		} else {
			Query query = _em
					.createNativeQuery("select nvl(count(*),0) from IncomeBranch t");
			return new BigInteger(query.getSingleResult().toString());
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<IncomeBranch> getAllIncomeBranchList(PCPSWebInput webInput) {
		String sql = "";
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = "select t.* from IncomeBranch t where t.IssueCompanyNo = '"
					+ issueCompanyNo + "'";
		} else {
			sql = "select t.* from IncomeBranch t";
		}
		sql = sql + " order by t.IncomeBranchId";
		Query query = _em.createNativeQuery(sql, IncomeBranch.class);
		return query.getResultList();
	}
	/**
	 * 根据id删除收益机构
	 */
	public void deleteIncomeBranch(IncomeBranch incomeBranch)throws PCPSApplicationException{
		GeneralDao.remove(_em, incomeBranch);
	}
	/**
	 * 根据id验证收益机构是否被引用
	 */
	public int isIncomeBranch(String issuecompanyno,String incomeBranchid)throws PCPSApplicationException{
		
		int isCount=0;
			Query query = _em
					.createNativeQuery("select nvl(count(*),0) from SaleBranch t where  t.incomebranchid='"+incomeBranchid+"'");
			isCount=Integer.parseInt(query.getSingleResult().toString());
			if(isCount==0){
				Query query1 = _em.createNativeQuery("select nvl(count(*),0) from Merchant t where t.IssueCompanyNo='"+issuecompanyno+"' and  t.incomebranchid='"+incomeBranchid+"'");
		         isCount=Integer.parseInt(query1.getSingleResult().toString());
			}
			
		return isCount;
		
	}
}